package com.ez2find.backing;

import com.ez2find.manager.FlagManageLocal;
import com.ez2find.manager.ForumManagerLocal;
import com.ez2find.manager.MessageManagerLocal;
import com.ez2find.manager.NickManagerBean;
import com.ez2find.model.Flag;
import com.ez2find.model.Message;
import com.ez2find.util.PageMessages;
import com.ez2find.util.SessionContext;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

public class FlagsBean {

    // <editor-fold defaultstate="collapsed" desc="Define variables">
    @EJB
    ForumManagerLocal ejbForums;
    @EJB
    FlagManageLocal ejbFlags;
    @EJB
    MessageManagerLocal ejbMessages;
    private Flag flag;
    private boolean newFlag;
    private Long forumId;
    private boolean viewFlag;
    private Message message;
    @Resource
    UserTransaction utx;
    // </editor-fold >

    public FlagsBean() {
    }

    public String saveFlag() {
        SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
        ForumsSession mBean = (ForumsSession) SessionContext.getAttribute("ForumsSession");
        System.out.println("Saving changes..");
        try {
            if ((getFlag() != null) && (getFlag().getId() != null)) {
              
                ejbFlags.update(flag);
                System.out.println("Flag for this message already created.");
                FacesMessage msg = new FacesMessage("Flag for this message already created.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                PageMessages pm=new PageMessages();
                //pm.setMessageImage("/images/error.png");
                SessionContext.setAttribute("PageMessages", pm);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return "flagSent";
            } else {
                flag.setUser(sessionBean.getEjbSession().getOwner());
                flag.setMessage(mBean.getCurrentMessage());
                ejbFlags.create(flag);
                sessionBean.getEjbSession().getOwner().getFlags().add(flag);
                System.out.println("Flag created correctly.");
                FacesMessage msg = new FacesMessage("Flag created correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                FacesContext.getCurrentInstance().addMessage(null, msg);

                //PageMessages pm=new PageMessages();
                //pm.setMessageImage("/images/correct.jpg");
                //SessionContext.setAttribute("PageMessages", pm);
                
                return "flagSent";
            }

        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage("Error creating/updating flag.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(FlagsBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String removeFlag() {
        try {
            SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            ejbFlags.delete(flag);
            sessionBean.getEjbSession().getOwner().getFlags().remove(flag);
            System.out.println("Flag removed");
            FacesMessage msg = new FacesMessage("Flag removed.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } catch (Exception ex) {
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public List<Flag> getFlags() {
        System.out.println("NUMERO DE FLAGS: " + message.getFlags().size());
        return message.getFlags();
    }

    public List<Message> getMessages() {
        return ejbMessages.listAll();
    }

    public List<Message> getFlagedMessages() {
        List<Message> fm = new ArrayList<Message>();
        for (Message m : getMessages()) {
            if (!m.getBanderotas().isEmpty()) {
                fm.add(m);
            }
        }
        return fm;
    }

    public String removeFlags() throws Exception {
        Flag[] banderas = message.getFlags().toArray(new Flag[0]);
        for (Flag f : banderas) {
            ejbFlags.delete(f);
            message.getFlags().remove(f);
            System.out.println("FLAG ELIMINADO");
        }
        //ejbFlags.delete(message.getFlags().get(0));
        //message.getFlags().remove(message.getFlags().get(0));
        ejbMessages.update(message);
        FacesMessage msg = new FacesMessage("Flags removed succesfully.");
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return null;
    }

    public String removeFlagsMessage() throws Exception {
        ejbMessages.delete(message);
        FacesMessage msg = new FacesMessage("Flags & message removed succesfully.");
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return null;
    }
    // <editor-fold defaultstate="collapsed" desc="Setters and getters"> 

    public Flag getFlag() {
        if (flag == null) {
            flag = new Flag();
        }

        return flag;
    }

    public void setFlag(Flag flag) {
        this.flag = flag;
    }

    public boolean isNewFlag() {
        return newFlag;
    }

    public void setNewFlag(boolean newFlag) {
        this.newFlag = newFlag;
    }

    public Long getForumId() {
        return forumId;
    }

    public void setForumId(Long forumId) {
        this.forumId = forumId;
    }

    public boolean isViewFlag() {
        return viewFlag;
    }

    public void setViewFlag(boolean viewFlag) {
        this.viewFlag = viewFlag;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

// </editor-fold>
}
